<?php
session_start();
include_once("auth_process.php");

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta property="og:url" content="https://secure.s3.ierg4210.ie.cuhk.edu.hk"/>
    <meta property="og:type" content="website"/>
    <meta property="og:title" content="Freewheel Online Shop"/>
    <meta property="og:description" content="IERG4210 online project"/>
    <meta property="og:image" content="https://secure.s3.ierg4210.ie.cuhk.edu.hk/images/freeonlineshop.png"/>
    <title>Home page of Freewheel Online Mall</title>
    <link href="../css/common.css" rel="stylesheet" type="text/css"/>
    <style>
        #nav_menu {
            padding-left: 70px;
        }

        #category_table {
            padding-left: 50px;
            padding-right: 50px;
            margin-top: 40px;
            margin-left: auto;
            margin-right: auto;
            display: block;
            padding-bottom: 300px;
        }

        .category_cell {
            display: inline-block;
            width: 260px;
            margin-left: 20px;
            margin-top: 30px;
        }

        .category_pic {
            width: 260px;
            height: 100px;
            display: block;
            margin: auto;
        }

        .category_name {
            text-align: center;
            padding-top: 0;
            margin-top: 5px;
            height: 50px;
        }

        .category_name a:link {
            color: black;
            text-decoration: none;
        }

        .category_name a:hover {
            color: black;
            text-decoration: none;
        }

        a:visited {
            color: black;
            text-decoration: none;
        }

        .fb-share-button {
            margin-top: 10px;
            margin-right: 50px;
            float: right;
        }

    </style>
</head>
<body>
<?php
$usrname = getCurrentUsername();
if ($usrname['result'] != true) {
    echo "<p  id='login_entry'><a href='login.php'>Hi, please login!</a></p>";
} else {
    echo "<p  id='login_entry'>Hello, " . htmlspecialchars($usrname['username']) . " | <a href='logout.php'>Log out</a></p>";
}
$cart_token = md5(time() . "cart_token" . rand(0, 9999));
$_SESSION['cart_token'] = $cart_token;
?>
<div class="fb-share-button"
     data-href="https://secure.s3.ierg4210.ie.cuhk.edu.hk"
     data-layout="button"
     data-size="small"
     data-mobile-iframe="true">
    <a class="fb-xfbml-parse-ignore" target="_blank"
       href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&amp;src=sdkpreparse">Share</a>
</div>

<div id="container">
    <?php readfile("../htmls/header.html") ?>
    <div id="body">
        <nav id="nav_menu">
            <a href="index.php">Home</a>
        </nav>

        <?php include_once("shopping_cart_form.php") ?>


        <section id="category_table">

            <?php
            include_once("db_connection.php");

            try {
                $conn = getdb();
                $stmt = $conn->prepare("SELECT catid , cname ,extension FROM categories");
                $stmt->execute();

                $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                $categories = $stmt->fetchAll();
                foreach ($categories as $k => $v) {
                    echo "<div class=\"category_cell\">
                        <a href=\"category_detail.php?catid=" . $v['catid'] . "\">
                            <img src = '../images/categories/category" . $v['catid'] . "." . htmlspecialchars($v['extension']) . "' class=\"category_pic\">
                        </a>
                        <p class=\"category_name\">
                            <a href=\"category_detail.php?catid=" . $v['catid'] . "\">" . htmlspecialchars($v['cname']) . "</a>
                        </p>
                    </div>" .
                        "\n";
                }

            } catch (PDOException $e) {
                echo "Error: " . $e->getMessage();
            }
            $conn = null;
            ?>


        </section>
    </div>
    <?php readfile("../htmls/footer.html") ?>
</div>
<script type="text/javascript" src="../javascripts/myLib.js"></script>
<script type="text/javascript" src="../javascripts/shopping_cart.js"></script>
<div id="fb-root"></div>
<script>(function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/zh_TW/sdk.js#xfbml=1&version=v2.8";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>

</body>
</html>

